<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="选择项目" prop="projectId">
        <el-select v-model="queryParams.projectId" @change="getSites">
          <el-option v-for="p in projectList" :key="p.id" :label="p.projectName" :value="p.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择场地" prop="siteId">
        <el-select v-model="queryParams.siteId" @change="handleQuery">
          <el-option v-for="s in siteList" :key="s.id" :label="s.name" :value="s.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预定人姓名" prop="orderName">
        <el-input
          v-model="queryParams.orderName"
          placeholder="请输入预定人姓名"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="orderTelno">
        <el-input
          v-model="queryParams.orderTelno"
          type="number"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预定时间" prop="orderDateRange">
        <el-date-picker
          v-model="orderDateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTimeRange">
        <el-date-picker
            v-model="payTimeRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单来源" prop="dataFrom">
        <el-select v-model="queryParams.dataFrom" clearable style="width: 240px" @change="handleQuery">
          <el-option label="小程序" value="0" />
          <el-option label="后台" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="预订类型" prop="orderType">
        <el-select v-model="queryParams.orderType" clearable style="width: 240px" @change="handleQuery">
          <el-option label="普通预定" value="0" />
          <el-option label="长租预定" value="1" />
          <el-option label="预留" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否支付" prop="payStatus">
        <el-select v-model="queryParams.payStatus" clearable style="width: 240px" @change="handleQuery">
          <el-option label="未支付" value="0" />
          <el-option label="已支付" value="1" />
          <el-option label="部分退款" value="2" />
          <el-option label="已退款" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" clearable style="width: 240px" @change="handleQuery">
          <el-option label="扫码" value="0" />
          <el-option label="现金" value="1" />
          <el-option label="余额" value="2" />
          <el-option label="银联" value="3" />
          <el-option label="支付宝" value="4" />
          <el-option label="微信" value="5" />
          <el-option label="微信支付" value="7" />
          <el-option label="其他" value="6" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button type="primary" plain icon="Plus" @click="handleAdd">场地预定</el-button>
        <el-button type="warning" plain @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 表格数据 -->
    <el-table
      v-loading="loading" :data="tableData" :tree-props="{ children: 'sonList'}"
      row-key="id" @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column label="序号" width="80" type="index" /> -->
      <el-table-column label="场地" prop="siteName" min-width="200" />
      <el-table-column label="项目" prop="projectName" min-width="120" />
      <el-table-column label="预订时间" min-width="220">
        <template #default="scope">
          <div v-if="scope.row.parentType!='1'">
            <span class="mr10">{{ scope.row.orderDate }}</span>
            <span>{{ scope.row.orderTimePeriod.split(',').join('~') }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="预订类型" prop="orderTypeStr" min-width="120" />
      <el-table-column label="会员姓名" prop="orderName" min-width="120" />
      <el-table-column label="手机号" prop="orderTelno" min-width="120" />
      <el-table-column label="会员卡类型" prop="memberCardName" min-width="120" />
      <el-table-column label="订单编号" prop="orderCode" min-width="180" />
      <el-table-column label="订场时长（小时）" prop="orderHour" min-width="140" />
      <el-table-column label="订单金额" prop="orderAmount" min-width="120" />
      <el-table-column label="优惠金额" min-width="120">
        <template #default="scope">
          <span>{{ decimalTo2(scope.row.orderAmount-scope.row.finalOrderAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折后金额" prop="finalOrderAmount" min-width="120" />
      <el-table-column label="退款金额" prop="refundAmount" min-width="120" />
      <el-table-column label="支付状态" prop="payStatusStr" min-width="120" />
      <el-table-column label="支付方式" prop="payTypeStr" min-width="120" />
      <el-table-column label="交易时间" prop="payTime" min-width="120" />
      <el-table-column label="操作员" prop="operatorName" min-width="120" />
      <el-table-column label="备注" prop="remark" min-width="200" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding" min-width="100">
        <template #default="scope">
          <el-button type="text" @click="viewRow(scope.row)">详情</el-button>
          <el-button v-if="scope.row.payStatus > '2' || scope.row.status!='1'" type="text" disabled>退订</el-button>
          <el-button v-else type="text" @click="unSub(scope.row)">退订</el-button>
          <!-- <el-button type="text" :disabled="!checkUnSub(scope.row)" @click="unSub(scope.row)">退订</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <!--详情-->
    <dialogViewReserve :props="dialogViewProps" />
    <!--退订-->
    <dialogUnSubscribe :props="dialogUnSubProps" @submitForm="getList" />
  </div>
</template>

<script setup>
import { getOrderList } from '../../../api/ven/reserve'
import { onMounted, reactive } from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import { getCurrentInstance } from 'vue'
import { getSiteList, projectConfigList } from '../../../api/ven'
import { useRouter } from 'vue-router'
import dialogViewReserve from './components/dialog/viewReserve'
import dialogUnSubscribe from './components/dialog/unsubscribe'
import { dayjs } from 'element-plus'
import { decimalTo2 } from '../../../utils/ruoyi'


const router = useRouter()
const { proxy } = getCurrentInstance()

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    notBlockOrder: '1',
    isParent: '1'
  },
  showSearch: true,
  loading: false,
  tableData: [],
  total: 0,
  selectedIds: [],
  multiple: true,
  projectList: [],
  siteList: [],
  orderDateRange: [],
  payTimeRange: []
})
const { queryParams, showSearch, loading, tableData, total, selectedIds, multiple, projectList, siteList, orderDateRange, payTimeRange } = toRefs(data)

const dialogPropsBase = {
  open: false,
  data: null
}
const dialogViewProps = reactive({
  ...dialogPropsBase
})
const dialogUnSubProps = reactive({
  ...dialogPropsBase
})


onMounted(() => {
  projectConfigList().then(res => {
    projectList.value = res.rows
  })

  getList()
})

function getSites() {
  queryParams.value.siteId = ''
  siteList.value = []
  getSiteList({
    projectId: queryParams.value.projectId
  }).then(res => {
    siteList.value = res.rows
  })

  handleQuery()
}


function getList() {
  loading.value = true
  getOrderList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  if (orderDateRange.value.length > 0) {
    queryParams.value.orderDateRange = orderDateRange.value.join(',')
  }
  if (payTimeRange.value.length > 0) {
    queryParams.value.payTimeRange = payTimeRange.value.join(',')
  }
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  orderDateRange.value = []
  payTimeRange.value = []
  handleQuery()
}

function handleAdd() {
  router.push('orderTable')
}

function handleSelectionChange(selection) {
  selectedIds.value = selection.map(item => item.id)
  multiple.value = !selection.length
}
function handleExport() {
  proxy.download('/systemj/siteOrder/export', {
    ...queryParams.value
  }, `reserve_${new Date().getTime()}.xlsx`)
}
function viewRow(row) {
  _.assign(dialogViewProps, dialogPropsBase, {
    open: true,
    data: { ...row }
  })
}

/**
 * 退订
 * @param row
 */
function unSub(row) {
  _.assign(dialogUnSubProps, dialogPropsBase, {
    open: true,
    data: { ...row }
  })
}

function checkUnSub(row) {
  let canBack = true
  if (!(row.payStatus === '0' || row.payStatus === '1')) {
    canBack = false
  }

  if (canBack) {
    const now = dayjs().valueOf()
    const orderDate = dayjs(`${row.orderDate} ${row.orderTimePeriod.split(',')[0]}`).valueOf()
    if (orderDate < now) {
      canBack = false
    }
  }

  return canBack
}

function batchUnSub() {
  console.log(selectedIds)
}

</script>

<style scoped>
	.mr10{margin-right:10px;}
</style>
